package org.jaaksi.androidforflutterdemo;

import android.app.Activity;
import android.util.Log;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.PluginRegistry;

// Android 主动向flutter发送信息
public class FlutterPluginCounter implements EventChannel.StreamHandler {

  public static String CHANNEL = "org.jaaksi/plugin";

  static EventChannel channel;

  private Activity activity;

  private FlutterPluginCounter(Activity activity) {
    this.activity = activity;
  }

  // 创建一个继承FlutterActivity，调用该方法注册
  public static void registerWith(PluginRegistry.Registrar registrar) {
    channel = new EventChannel(registrar.messenger(), CHANNEL);
    FlutterPluginCounter instance = new FlutterPluginCounter(registrar.activity());
    channel.setStreamHandler(instance);
  }

  @Override
  public void onListen(Object o, final EventChannel.EventSink eventSink) {

    eventSink.success("成功啦");
    eventSink.error("-1", "失败啦", "没网络啦");
  }

  @Override
  public void onCancel(Object o) {
    Log.i("FlutterPluginCounter", "FlutterPluginCounter:onCancel");
  }
}
